import io
import os
import sys
import traceback

import facade
from PIL import Image
from xjlibrary.mrequest.baserequest import BaseRequest
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "cdxg_ams", "download", "cover")

BaseUrl = "https://journals.ametsoc.org"
HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
           'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive',
           'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'max-age=0',
           'Host': 'journals.ametsoc.org',
           'Upgrade-Insecure-Requests': '1',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/66.0.3359.139 Safari/537.36', }
ListSqls = []
nCount = 0
configfile = BaseDir.get_new_path(curPath, "db.ini")
logger = facade.get_streamlogger()
mysqlutils = facade.MysqlUtiles(configfile, "db", logger)


def SelectListFromDB():
    global nCount, ListSqls
    sSql = "SELECT `id`,`url`,`imageurl` FROM `ams_journal`"
    rows = mysqlutils.SelectFromDB(sSql)
    return rows


# 测试文字输出到文本
def output(value, files='log.txt'):
    BaseDir.single_write_file(value, files)


def get_url(url):
    print("url is : {}".format(url))
    BoolResult, errString, r = BaseRequest(url, headers=HEADERS, endstring="", timeout=30,verify=False)
    if not BoolResult:
        print("出现错误 ，请检查")
        sys.exit(-1)
    return r


def Img2Jpg(buf, dstFile):
    print('Img2Jpg %s ...' % dstFile)

    exMsg = ''
    try:
        srcImg = Image.open(io.BytesIO(buf))
        dstImg = srcImg.resize((108, 150), Image.ANTIALIAS).convert('RGB')
        dstImg.save(dstFile, 'JPEG')
    except:
        exMsg = '* %s' % traceback.format_exc()
        print(exMsg)
    if exMsg:
        return False

    return True


def save_file(name, r):
    outfile = "{}/{}".format(sPath, name)
    if not os.path.exists(sPath):
        os.makedirs(sPath)
    if os.path.exists(outfile):
        print("文件存在{}".format(outfile))
        return
    if Img2Jpg(r.content, outfile):
        print("保存图片成功")
    else:
        print("保存文件失败,请检查")
        sys.exit(-1)


# 插入数据库
def InsertIntoDbFromList():
    global nCount, ListSqls
    mysqlutils.ExeSqlListToDB(ListSqls)
    ListSqls = list()


def get_list_url(rows):
    for id_, url, imageurl in rows:
        # 这个特殊 结构不一样
        names = url.split("/")[-2]
        name = names + ".jpg"  # "celljournal@"+name+"★/smartlib/celljournal/"
        outfile = "{}/{}".format(sPath, name)

        if not os.path.exists(sPath):
            os.makedirs(sPath)
        if os.path.exists(outfile):
            continue
        urlimage = BaseUrl + imageurl
        r = get_url(urlimage)
        print("下载完成开始解析并下载保存图片")
        save_file(name, r)

        if len(ListSqls) > 100:
            InsertIntoDbFromList()


def main():
    rows = SelectListFromDB()
    get_list_url(rows)


if __name__ == "__main__":
    main()